import express, { Request, Response } from 'express'
import {  } from 'mysql'
import emailSend from "../utils/email";
import {
    passwordLoginService,
    userAddService,
    userResetService,
    codeLoginService,
    tokenInfoService
} from "../service/userService"
const router = express.Router()
/*
* 发送邮件获取验证码
* @param email string 邮箱
* @param mode string 类型
* */
router.post('/email', (req: Request, res: Response) => {
    const { email, mode } = req.body
    emailSend(res, email, mode)
})
/*
* 注册
* @param email string 邮箱
* @param code 验证码
* @param mode 类型
* */
router.post('/register',  async (req: Request, res: Response) => {
    const { mode, code, email } = req.body
    await userAddService(res, mode, code, email)
})
/*
* 重置密码
* @param email string 邮箱
* @param code 验证码
* @param mode 类型
* @param password string 新密码
* */
router.post('/reset',  async (req: Request, res: Response) => {
    const { email, code, mode, password } = req.body
    await userResetService(res, mode, code, email, password)
})
/*
* 密码登录
* @param email string 邮箱
* @param password string 密码
* */
router.post('/password/login', async (req: Request, res: Response) => {
    const { email, password } = req.body
    await passwordLoginService(res, email, password)
})
/*
* 验证码登录
* @param email string 邮箱
* @param code string 验证码
* @parma mode string 类型
* */
router.post('/email/login',  async (req: Request, res: Response) => {
    const { email, code, mode } = req.body
    await codeLoginService(res, email, code, mode)
})
/*
* token获取用户信息
* */
router.post('/token/info',  async (req: Request, res: Response) => {
    const token = req.headers[process.env.NODE_MANAGE_TOKEN as string]
    await tokenInfoService(res, token)
})

export default router